//
//  HJDPofileController.m
//  JYHospital
//
//  Created by 胡金东 on 16/3/31.
//  Copyright © 2016年 胡金东. All rights reserved.
//

#import "HJDBBSController.h"
#import "RESideMenu.h"

#import "HJDTextViewLbl.h"
#import "UWDatePickerView.h"
#import "MBProgressHUD.h"
#import "AFNetworking.h"


@interface HJDBBSController ()<UITextViewDelegate,UWDatePickerViewDelegate,UIScrollViewDelegate>
{
    UWDatePickerView * _pickerView;
}

@property (nonatomic,strong) UIScrollView * scroll;

@property (nonatomic,strong)  UIView * marginView;
@property (nonatomic,strong)  UIView * marginVieww;
@property (nonatomic,strong)  UIView * marginViewww;

@property (nonatomic,weak) HJDTextViewLbl *textViewLbl;

@property (nonatomic,strong) UILabel *timeLbl;

@property (nonatomic,strong) NSString * date;
@property (nonatomic,strong) NSCalendar *cal;
@property (nonatomic,assign) NSInteger flags;
//名字
@property (nonatomic,strong) UITextField  *nameField ;
//电话
@property (nonatomic,strong) UITextField  *phonetextField;
//qq
@property (nonatomic,strong)  UITextField  *textField ;
//咨询内容
@property (nonatomic,strong) HJDTextViewLbl  *resultTextView ;


@property (nonatomic,strong) UIButton *timeBtn;



@end

@implementation HJDBBSController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setUpNavigation];

    
    
}

-(void)setUpNavigation
{
    self.scroll = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.scroll.delegate = self;
    self.automaticallyAdjustsScrollViewInsets = YES;
    self.scroll.showsHorizontalScrollIndicator = NO;
    self.scroll.showsVerticalScrollIndicator = YES;
    self.scroll.contentSize = CGSizeMake(HJDWidth,680);
    [self.view addSubview:self.scroll];
    
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(HJDWidth/2 -40, 20, 80, 20)];
    title.text = @"一键挂号";
    title.textColor = [UIColor getColorWithHexNumber:@"848484"];
    title.font = [UIFont systemFontOfSize:19];
    [self.scroll addSubview:title];

    
    CGFloat leftX = HJDHorizontalMargin;
    CGFloat leftY = 30 - 10;
    CGFloat leftW = 34;
    CGFloat leftH = 34;
    UIButton *navleftBtn = [[UIButton alloc]initWithFrame:CGRectMake(leftX, leftY, leftW, leftH)];
    [navleftBtn setBackgroundImage:[UIImage imageNamed:@"矢量智能对象侧"] forState:UIControlStateNormal];
    [navleftBtn addTarget:self action:@selector(MyCenterClick) forControlEvents:UIControlEventTouchUpInside];

//    [self.scroll addSubview:navleftBtn];
    [self setUpInfo];
    [self setUpResult];

    
    [self setUpBtn];
    CGFloat timeLblX = HJDHorizontalMargin + 80;
    CGFloat timeLblY = CGRectGetMaxY(self.marginVieww.frame) + 10;
    CGFloat timeLblW = 150;
    CGFloat timeLblH = 40;
    self.timeLbl = [[UILabel alloc]initWithFrame:CGRectMake(timeLblX, timeLblY, timeLblW, timeLblH)];
    self.timeLbl.text = @"请选择预约时间";
    self.timeLbl.textColor = [UIColor grayColor];
    
    [self.scroll addSubview:self.timeLbl];
    //添加手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    tap.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tap];
    
    
    
        

}

//隐藏键盘
-(void)viewTapped:(UITapGestureRecognizer*)tap
{
    
    [self.view endEditing:YES];
    
}

-(void)MyCenterClick{
    
[self.sideMenuViewController presentLeftMenuViewController];
}

-(void)setUpInfo
{
    CGFloat nameX = HJDHorizontalMargin;
    CGFloat nameY = HJDVerticalMargin*2 + 10;
    CGFloat nameW = HJDWidth - HJDHorizontalMargin;
    CGFloat nameH = 40;
    self.nameField = [[UITextField alloc]initWithFrame:CGRectMake(nameX, nameY, nameW, nameH)];
    self.nameField.clearButtonMode = UITextFieldViewModeAlways;
    self.nameField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40,50 )];
    self.nameField.leftViewMode = UITextFieldViewModeAlways;
    UILabel *namelbl = [[UILabel alloc]initWithFrame:CGRectMake(0,0, 40, 50)];
    namelbl.text = @"姓名:";
    namelbl.textColor = [UIColor getColorWithHexNumber:@"303030"] ;
    [self.nameField.leftView addSubview:namelbl];
    [self.scroll addSubview:self.nameField];
    
    //    横线
    CGFloat viewX = HJDHorizontalMargin ;
    CGFloat viewY = CGRectGetMaxY(self.nameField.frame);
    CGFloat viewW = HJDWidth - HJDHorizontalMargin ;
    CGFloat viewH = 1;
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(viewX, viewY, viewW, viewH)];
    view.backgroundColor = [UIColor getColorWithHexNumber:@"F1F1EE"];
    [self.scroll addSubview:view];
    
    CGFloat phoneX = HJDHorizontalMargin  ;
    CGFloat phoneY = CGRectGetMaxY(view.frame)+5;
    CGFloat phoneW = HJDWidth - HJDHorizontalMargin;
    CGFloat phoneH = 40;
    self.phonetextField = [[UITextField alloc]initWithFrame:CGRectMake(phoneX, phoneY,phoneW, phoneH)];
    self.phonetextField.clearButtonMode = UITextFieldViewModeAlways;
    self.phonetextField.keyboardType = UIKeyboardTypeNumberPad;
    self.phonetextField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40,50 )];
    self.phonetextField.leftViewMode = UITextFieldViewModeAlways;
    UILabel *phonelbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 40, 50)];
    phonelbl.text = @"电话:";
    phonelbl.textColor = [UIColor getColorWithHexNumber:@"303030"] ;
    [self.phonetextField.leftView addSubview:phonelbl];
    
    [self.scroll addSubview:self.phonetextField];
    //    横线
    CGFloat phoneviewX = HJDHorizontalMargin ;
    CGFloat phoneviewY = CGRectGetMaxY(self.phonetextField.frame);
    CGFloat phoneviewW = HJDWidth - HJDHorizontalMargin ;
    CGFloat phoneviewH = 1;
    UIView *phoneview = [[UIView alloc]initWithFrame:CGRectMake(phoneviewX, phoneviewY, phoneviewW, phoneviewH)];
    phoneview.backgroundColor = [UIColor getColorWithHexNumber:@"F1F1EE"];
    [self.scroll addSubview:phoneview];
    CGFloat X = HJDHorizontalMargin  ;
    CGFloat Y = CGRectGetMaxY(phoneview.frame);
    CGFloat W = HJDWidth - HJDHorizontalMargin ;
    CGFloat H = 40;
    self.textField = [[UITextField alloc]initWithFrame:CGRectMake(X, Y, W, H)];
    self.textField.keyboardType = UIKeyboardTypeNumberPad;
    self.textField.clearButtonMode = UITextFieldViewModeAlways;
    self.textField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40,50 )];
    self.textField.leftViewMode = UITextFieldViewModeAlways;
    UILabel *textlbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 40, 50)];
    textlbl.text = @"Q  Q:";
    textlbl.textColor = [UIColor getColorWithHexNumber:@"303030"] ;
    [ self.textField.leftView addSubview:textlbl];
    
    [self.scroll addSubview: self.textField];
    
    
    CGFloat marginViewX = 0 ;
    CGFloat marginViewY = CGRectGetMaxY( self.textField.frame)+5;
    CGFloat marginViewW = HJDWidth;
    CGFloat marginViewH = HJDVerticalMargin;
    self.marginView = [[UIView alloc]initWithFrame:CGRectMake(marginViewX, marginViewY, marginViewW, marginViewH)];
    self.marginView.backgroundColor = [UIColor getColorWithHexNumber:@"F1F1EE"];
    [self.scroll addSubview:self.marginView];
    
}


-(void)setUpResult
{
    CGFloat lblX = HJDHorizontalMargin ;
    CGFloat lblY = CGRectGetMaxY(self.marginView.frame) ;
    CGFloat lblW = HJDWidth - HJDHorizontalMargin ;
    CGFloat lblH = 40;
    UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(lblX,lblY, lblW, lblH)];
    lbl.text =@"咨询内容";
    lbl.textColor = [UIColor getColorWithHexNumber:@"303030"];
    lbl.font = [UIFont systemFontOfSize:18];
    [self.scroll addSubview:lbl];
    
    CGFloat resultX = HJDHorizontalMargin ;
    CGFloat resultY = CGRectGetMaxY(lbl.frame) + 10 ;
    CGFloat resultW = HJDWidth - HJDHorizontalMargin * 2 + HJDHorizontalMargin/4;
    CGFloat resultH = 150;
    self.resultTextView = [[HJDTextViewLbl alloc]initWithFrame:CGRectMake(resultX, resultY, resultW, resultH)];
    _textViewLbl = self.resultTextView;
    self.resultTextView.placeHolder = @"例如：酸辣粉就算了弗利萨法拉利发了发了发蜡水立方萨拉放家里撒放家里撒";
    //    resultTextView.text = @"lsflasnlkflsamfsalkfalsfljsalfjlksjfljsalfjsjsjflsajlfjlsaflsj立刻就死了就发啦数据库来房间辣椒了房间爱令肌肤可垃圾费拉拉看空间发垃圾分类";
    self.resultTextView.font = [UIFont systemFontOfSize:13];
    
    self.resultTextView.backgroundColor = [UIColor getColorWithHexNumber:@"F1F1EE"];
    [self.resultTextView setRadius:10 borderWith:0 borderColor:nil];
    
    //    垂直拖拽
    self.resultTextView.alwaysBounceVertical = YES;
    self.resultTextView.delegate = self;
    [self.scroll addSubview:self.resultTextView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextViewTextDidChangeNotification object:nil];
    
    CGFloat marginViewwX = 0 ;
    CGFloat marginViewwY = CGRectGetMaxY(self.resultTextView.frame)+15;
    CGFloat marginViewwW = HJDWidth;
    CGFloat marginViewwH = HJDVerticalMargin;
    self.marginVieww = [[UIView alloc]initWithFrame:CGRectMake(marginViewwX, marginViewwY, marginViewwW, marginViewwH)];
    self.marginVieww.backgroundColor = [UIColor getColorWithHexNumber:@"F1F1EE"];
    [self.scroll addSubview:self.marginVieww];
    
    CGFloat timeBtnX = HJDHorizontalMargin;
    CGFloat timeBtnY = CGRectGetMaxY(self.marginVieww.frame) + 10;
    CGFloat timeBtnW = 290;
    CGFloat timeBtnH = 40;
    self.timeBtn = [[UIButton alloc]initWithFrame:CGRectMake(timeBtnX, timeBtnY, timeBtnW, timeBtnH)];
    [self.timeBtn addTarget:self action:@selector(timeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.timeBtn setTitle:@"预约时间：" forState:UIControlStateNormal];
    [self.timeBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 200)];
    [self.timeBtn setTitleColor:[UIColor getColorWithHexNumber:@"303030"] forState:UIControlStateNormal];
    [self.scroll addSubview:self.timeBtn];
    
    
    
    
}

-(void)setUpBtn
{
    
    CGFloat marginViewwX = 0 ;
    CGFloat marginViewwY = CGRectGetMaxY(self.timeBtn.frame) + HJDVerticalMargin/2;
    CGFloat marginViewwW = HJDWidth;
    CGFloat marginViewwH = HJDVerticalMargin*2;
    self.marginViewww = [[UIView alloc]initWithFrame:CGRectMake(marginViewwX, marginViewwY, marginViewwW, marginViewwH)];
    self.marginViewww.backgroundColor = [UIColor getColorWithHexNumber:@"F1F1EE"];
    [self.scroll addSubview:self.marginViewww];
    
    CGFloat lblX = HJDWidth - 220 -10 ;
    CGFloat lblY = marginViewwY + HJDVerticalMargin/3;
    CGFloat lblW = 220;
    CGFloat lblH = 30;
    UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(lblX, lblY, lblW, lblH)];
    lbl.text = @"请务必准时,避免错过您的最佳治疗时间";
    lbl.textColor = [UIColor getColorWithHexNumber:@"72726F"];
    lbl.font = [UIFont systemFontOfSize:12];
    [self.scroll addSubview:lbl];
    
    
    
    UIButton *saveBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 550, HJDWidth, 50)];
    [saveBtn setTitle:@"提交订单" forState:UIControlStateNormal];
    saveBtn.titleLabel.font = [UIFont systemFontOfSize:19];
    [saveBtn setBackgroundColor:[UIColor getColorWithHexNumber:@"5CC6AB"]];
    [saveBtn addTarget:self action:@selector(saveBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.scroll addSubview:saveBtn];
}



//开始拖拽，键盘就消失
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    //    让textView中的键盘就消失，自定义的工具条就会消失
    [self.scroll endEditing:YES];
}




-(void)textChange
{
    // 判断下textView有木有内容
    if (_textViewLbl.text.length) {// 有内容
        _textViewLbl.hidePlaceHolder = YES;
        
    }else{
        _textViewLbl.hidePlaceHolder = NO;
    }
}




//选择时间
-(void)timeBtnClick
{
    [self setupDateView:DateTypeOfStart];
    
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    self.cal=[NSCalendar currentCalendar];
    _flags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday |
    NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    self.date = [formatter stringFromDate:[NSDate date]];
    HJDLog(@"手机当前时间%@",self.date);
    
}

- (void)setupDateView:(DateType)type {
    
    _pickerView = [UWDatePickerView instanceDatePickerView];
    _pickerView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    [_pickerView setBackgroundColor:[UIColor greenColor]];
    _pickerView.delegate = self;
    _pickerView.type = type;
    [self.view addSubview:_pickerView];
    
}

- (void)getSelectDate:(NSString *)date type:(DateType)type {
    switch (type) {
        case DateTypeOfStart:
            // TODO 日期确定选择
            break;
            
        case DateTypeOfEnd:
            // TODO 日期取消选择
            break;
        default:
            break;
    }
    int ci;
    NSComparisonResult result = [self.date compare:date];
    switch (result)
    {
            //date比self.date大
        case NSOrderedAscending:
            self.timeLbl.text = date;
            ci=1; break;
            //date比self.date小
        case NSOrderedDescending:
            [MBProgressHUD showError:@"时间过期 不可预约"];
            ci=-1; break;
            //date=self.date
        case NSOrderedSame:
            self.timeLbl.text = date;
            ci=0; break;
        default:
            break;
    }
    
    
}

-(void)saveBtnClick
{
    
        NSString *phone = @"^1+[34578]+\\d{9}";
        NSPredicate *pr = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phone];
    
        //    NSLog(@"提交订单");
        //名字
        if (self.nameField.text.length != 0) {
            //电话
            if (self.phonetextField.text.length != 0) {
                //手机号
                if ([pr evaluateWithObject:self.phonetextField.text ]) {
                        //咨询内容
                        if (self.resultTextView.text.length != 0) {
    
                            if (![self.timeLbl.text isEqualToString:@"请选择预约时间"]) {
                                NSUserDefaults *idDe = [NSUserDefaults standardUserDefaults];
                                NSString *ID = [idDe objectForKey:@"Id"];
                                
                                NSString *url = @"/app/registration.do";
                                NSString *path = [Hurl stringByAppendingString:url];
//                                NSString *path = @"http://gh.120hfz.com:8091/app_czgh.php";
                                AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
                                NSMutableDictionary *params = [NSMutableDictionary dictionary];
                               
                                params[@"userId"] = ID;
                                params[@"phone"]= self.phonetextField.text;
                                params[@"qq"] = self.textField.text;
                                params[@"time"] = self.timeLbl.text;
                                params[@"marks"] = self.resultTextView.text;
                                params[@"name"] = self.nameField.text;

//                                params[@"tel"]= self.phonetextField.text;
//                                params[@"userId"] = ID;
//                                params[@"qq"] = self.textField.text;
//                                params[@"order_date"] = self.timeLbl.text;
//                                params[@"content"] = self.resultTextView.text;
//                                params[@"name"] = self.nameField.text;
    
                                [mgr POST:path parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
    
                                } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                                    NSDictionary *info = (NSDictionary *)responseObject;
                                    NSString *successfolStr = info[@"resultCode"];
                                    if ([successfolStr integerValue] ==0) {
                                        [MBProgressHUD showSuccess:@"预约成功"];
                                        [self dismissViewControllerAnimated:YES completion:nil];
                                        
                                    }
                                    if ([successfolStr integerValue]==999) {
                                        [MBProgressHUD showError:@"系统异常"];
                                    }
    
                                } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                                    [MBProgressHUD showError:@"服务器升级，请稍后再试"];
    
                                }];
    
                            }else{
                                [MBProgressHUD showError:@"时间没有选择"];
                            }
    
                        }else{
                            [MBProgressHUD showError:@"咨询内容不可为空"];
                        }
               
                }else{
                    [MBProgressHUD showError:@"无效手机号码"];
                }
            }else{
                
                [MBProgressHUD showError:@"电话不可为空"];
                
            }
            
        }else{
            [MBProgressHUD showError:@"姓名不可为空"];
        }

}



@end
